﻿using System;
using System.Web;
using Newtonsoft.Json;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using eCommerce.db.DataRespository;
using eCommerce.log;

namespace eCommerce.db.Action
{
    public class CustomerAction : IBaseAction
    {
        public void Do(HttpContext context)
        {
            var actionName = Utils.GetIdFromRequestString(Utils.Action, string.Empty);

            var responseData = new ResponseData();
            switch (actionName)
            {
                case "searchbuyandsell":
                    responseData = SearchBuyAndSell();
                    break;
                case "search":
                    responseData = Search();
                    break;

                case "getcustomer":
                    responseData = GetCustomer();
                    break;

                case "changestatus":
                    responseData = ChangeStatus();
                    break;
                case "save":
                    responseData = Save();
                    break;
                case "delete":
                    responseData = Delete();
                    break;

                case "":
                    responseData.Success = false;
                    responseData.Message = "Can not found this action.";
                    break;
            }

            var jSonString = JsonConvert.SerializeObject(responseData, Utils.SerializeFormat("MM/dd/yyyy HH:mm:ss"));
            if (!string.IsNullOrEmpty(context.Request.Params["callback"]))
                jSonString = string.Format("{0}({1})", context.Request.Params["callback"], jSonString);
            context.Response.Write(jSonString);
        }

        public static ResponseData SearchBuyAndSell()
        {
            var responseData = new ResponseData();
            var keyword         = Utils.GetIdFromRequestString(Utils.Keyword, string.Empty);
            var isExactly       = Utils.GetIdFromRequestString(Utils.isExactly, false);
            var isAdministrator = Utils.GetIdFromRequestString(Utils.IsAdministrator, false);
            var storeId         = Utils.GetIdFromRequestString(Utils.StoreId, -1);                        
            var pageIndex       = Utils.GetIdFromRequestString(Utils.PageIndex, 0);
            var pageSize        = Utils.GetIdFromRequestString(Utils.PageSize, 0);

            try
            {
                var totalRecords = 0;
                var lstCustomer = CustomerBo.SearchBuyAndSell(keyword, storeId, isExactly, isAdministrator, pageIndex, Constant.SmallPageSize, ref totalRecords);

                for (int i = 0; i < lstCustomer.Count; i++)
                {
                    lstCustomer[i].ItemIndex          = Utils.CalcItemIndex(i, pageIndex, pageSize);
                    lstCustomer[i].TotalOrderAndMoney = OrderBo.GetTotalOrderAndMoneyByCustomer(lstCustomer[i].CustomerId);
                    switch (lstCustomer[i].IsActive)
                    {
                        case false:
                            lstCustomer[i].IconStatus = "<a class='btn14 topDir' title='Tạm dừng'><img src='/images/icons/dark/denied.png' alt=''/></a>";
                            break;
                        case true:
                            lstCustomer[i].IconStatus = "<a class='btn14 topDir' title='Kích hoạt'><img src='/images/icons/dark/check.png' alt=''/></a>";
                            break;                        
                    }                    
                }

                responseData.Success  = true;
                responseData.Data     = lstCustomer;
                responseData.TotalRow = totalRecords;
                responseData.Pager    = PagerHelper.PageLinks(pageIndex, Constant.SmallPageSize, totalRecords, Constant.PageStep, UrlHelper.PagerCustomerBuySell);  
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
           
            return responseData;
        }

        private static ResponseData Search()
        {
            var responseData = new ResponseData();
            var keyword     = Utils.GetIdFromRequestString(Utils.Keyword, string.Empty);
            var storeId     = Utils.GetIdFromRequestString(Utils.StoreId, -1);
            var status      = Utils.GetIdFromRequestString(Utils.Status, 0);
            var orderBy     = Utils.GetIdFromRequestString(Utils.OrderBy, 0);
            var pageIndex   = Utils.GetIdFromRequestString(Utils.PageIndex, 0);
            var pageSize    = Utils.GetIdFromRequestString(Utils.PageSize, 0);

            try
            {
                var totalRecords = 0;
                var lstCustomer = CustomerBo.Search(keyword, storeId, status, orderBy, pageIndex, pageSize, ref totalRecords);

                for (int i = 0; i < lstCustomer.Count; i++)
                {
                    lstCustomer[i].ItemIndex = Utils.CalcItemIndex(i, pageIndex, pageSize);
                    lstCustomer[i].TotalOrderAndMoney = OrderBo.GetTotalOrderAndMoneyByCustomer(lstCustomer[i].CustomerId);
                    switch (lstCustomer[i].IsActive)
                    {
                        case false:
                            lstCustomer[i].IconStatus = "<a class='btn14 topDir' title='Tạm dừng'><img src='/images/icons/dark/denied.png' alt=''/></a>";
                            break;
                        case true:
                            lstCustomer[i].IconStatus = "<a class='btn14 topDir' title='Kích hoạt'><img src='/images/icons/dark/check.png' alt=''/></a>";
                            break;                        
                    }                    
                }

                responseData.Success  = true;
                responseData.Data     = lstCustomer;
                responseData.TotalRow = totalRecords;
                responseData.Pager    = PagerHelper.PageLinks(pageIndex, pageSize, totalRecords, Constant.PageStep, UrlHelper.PagerCustomer);  
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
           
            return responseData;
        }

        private static ResponseData GetCustomer()
        {
            var responseData = new ResponseData();
            var id = Utils.GetIdFromRequestString(Utils.CustomerId, 0);

            try
            {
                var customer = CustomerBo.GetById(id);

                responseData.Success = true;
                responseData.Data = customer;
                responseData.TotalRow = 1;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }

        private static ResponseData ChangeStatus()
        {
            var responseData = new ResponseData();
            
            var id       = Utils.GetIdFromRequestString(Utils.CustomerId, 0);
            var status   = Utils.GetIdFromRequestString(Utils.Status, 0);

            try
            {
                CustomerBo.ChangeStatus(id, status);

                responseData.Success  = true;
                responseData.Data     = null;
                responseData.TotalRow = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }   

        private static ResponseData Save()
        {
            var responseData = new ResponseData();

            var id          = Utils.GetIdFromRequestString(Utils.CustomerId, 0);
            var storeId     = Utils.GetIdFromRequestString(Utils.StoreId, 0);
            var contactName = Utils.GetIdFromRequestString(Utils.Name, string.Empty);
            var mobile      = Utils.GetIdFromRequestString(Utils.Mobile, string.Empty);
            var email       = Utils.GetIdFromRequestString(Utils.Email, string.Empty);
            var address     = Utils.GetIdFromRequestString(Utils.Address, string.Empty);
            var desc        = Utils.GetIdFromRequestString(Utils.Description, string.Empty);            
            
            try
            {
                CustomerEntity o = null;
                if (id > 0)
                {
                    o = CustomerBo.GetById(id);
                    o.CustomerId    = id;
                    o.ContactName   = contactName;
                    o.Mobile        = mobile;
                    o.Email         = email;
                    o.Address       = address;
                    o.Description   = desc;
                }
                else
                {
                    o = new CustomerEntity();
                    o.StoreId       = storeId;
                    o.ContactName   = contactName;
                    o.Mobile        = mobile;
                    o.Email         = email;
                    o.Address       = address;
                    o.Description   = desc;
                    o.IsActive      = true;
                }

                var customerId = CustomerBo.Save(o);

                responseData.Success    = true;
                responseData.Data       = customerId;
                responseData.TotalRow   = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }

        private static ResponseData Delete()
        {
            var responseData = new ResponseData();

            var id = Utils.GetIdFromRequestString(Utils.CustomerId, 0);

            try
            {
                CustomerBo.Delete(id);

                responseData.Success = true;
                responseData.Data = null;
                responseData.TotalRow = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }
    }
}
